package cn.bellychang.qinchao.fibonacci;

/**
 * @author ChangLiang
 * @date 2020/9/6
 */
public class Solution {

    /**
     * 总执行语句数
     */
    private int count = 0;

    public int getCount() {
        return count;
    }

    public int fib(int n) {
        if (n < 0) {
            throw new RuntimeException("input should not be less than 0");
        }
        if (n == 0 || n==1) {
            count++;
            return n;
        }
        count++;
        return fib(n - 1) + fib(n-2);
    }

    static class SolutionDecorator extends Solution {
        private Solution solution;

        public SolutionDecorator(Solution solution) {
            this.solution = solution;
        }

        @Override
        public int fib(int n) {
            int fib = this.solution.fib(n);
            System.out.println("fibonacci n = " + n + " 时 所执行的代码行数 = " + solution.getCount());
            return fib;
        }
    }
}
